import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15
import "components"
import "styles"

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 1024
    height: 600
    title: "浮游菌采样器控制系统"
    
    // 禁用窗口调整大小（嵌入式系统固定分辨率）
    minimumWidth: 1024
    maximumWidth: 1024
    minimumHeight: 600
    maximumHeight: 600
    
    // 应用样式
    AppStyle {
        id: appStyle
    }
    
    // 主要内容区域
    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: qmlBridge.isUserLoggedIn ? mainDashboard : loginPage
        
        // 页面切换动画
        pushEnter: Transition {
            PropertyAnimation {
                property: "opacity"
                from: 0
                to: 1
                duration: 300
            }
        }
        
        pushExit: Transition {
            PropertyAnimation {
                property: "opacity"
                from: 1
                to: 0
                duration: 300
            }
        }
    }
    
    // 登录页面组件
    Component {
        id: loginPage
        LoginPage {
            onLoginSuccess: {
                stackView.replace(mainDashboard)
            }
        }
    }
    
    // 主仪表板组件
    Component {
        id: mainDashboard
        MainDashboard {
            onNavigateToPage: function(pageName) {
                switch(pageName) {
                    case "deviceControl":
                        stackView.push(deviceControlPage)
                        break
                    case "timedSampling":
                        stackView.push(timedSamplingPage)
                        break
                    case "volumeSampling":
                        stackView.push(volumeSamplingPage)
                        break
                    case "userManagement":
                        if (qmlBridge.hasUserManagement) {
                            stackView.push(userManagementPage)
                        } else {
                            showNotification("权限不足", "您没有用户管理权限", 2)
                        }
                        break
                    case "operationRecords":
                        stackView.push(operationRecordsPage)
                        break
                    case "dataVisualization":
                        stackView.push(dataVisualizationPage)
                        break
                    case "settings":
                        stackView.push(settingsPage)
                        break
                }
            }
            
            onLogoutRequested: {
                qmlBridge.logoutUser()
                stackView.replace(loginPage)
            }
        }
    }
    
    // 设备控制页面组件
    Component {
        id: deviceControlPage
        DeviceControlPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 定时采样页面组件
    Component {
        id: timedSamplingPage
        TimedSamplingPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 定量采样页面组件
    Component {
        id: volumeSamplingPage
        VolumeSamplingPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 用户管理页面组件
    Component {
        id: userManagementPage
        UserManagementPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 操作记录页面组件
    Component {
        id: operationRecordsPage
        OperationRecordsPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 数据可视化页面组件
    Component {
        id: dataVisualizationPage
        DataVisualizationPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 设置页面组件
    Component {
        id: settingsPage
        SettingsPage {
            onBackRequested: stackView.pop()
        }
    }
    
    // 通知对话框
    NotificationDialog {
        id: notificationDialog
    }
    
    // 确认对话框
    ConfirmDialog {
        id: confirmDialog
    }
    
    // 全局函数
    function showNotification(title, message, type) {
        notificationDialog.show(title, message, type)
    }
    
    function showConfirmDialog(title, message, callback) {
        confirmDialog.show(title, message, callback)
    }
    
    // 连接QML Bridge信号
    Connections {
        target: qmlBridge
        
        function onUserLoginStatusChanged(isLoggedIn) {
            if (isLoggedIn) {
                stackView.replace(mainDashboard)
            } else {
                stackView.replace(loginPage)
            }
        }
        
        function onNotificationRequested(title, message, type) {
            showNotification(title, message, type)
        }
        
        function onConfirmDialogRequested(title, message, callback) {
            showConfirmDialog(title, message, callback)
        }
        
        function onSystemError(error) {
            showNotification("系统错误", error, 2)
        }
    }
    
    // 键盘事件处理
    Keys.onPressed: function(event) {
        if (event.key === Qt.Key_F1) {
            // F1键显示帮助
            showNotification("帮助", "F1-帮助 F2-设备控制 F3-采样管理 F4-用户管理 ESC-返回", 0)
            event.accepted = true
        } else if (event.key === Qt.Key_F2) {
            // F2键快速进入设备控制
            if (qmlBridge.hasDeviceControl) {
                stackView.push(deviceControlPage)
            }
            event.accepted = true
        } else if (event.key === Qt.Key_F3) {
            // F3键快速进入采样管理
            stackView.push(timedSamplingPage)
            event.accepted = true
        } else if (event.key === Qt.Key_F4) {
            // F4键快速进入用户管理
            if (qmlBridge.hasUserManagement) {
                stackView.push(userManagementPage)
            }
            event.accepted = true
        } else if (event.key === Qt.Key_Escape) {
            // ESC键返回上一页
            if (stackView.depth > 1) {
                stackView.pop()
            }
            event.accepted = true
        }
    }
    
    // 应用程序状态管理
    Component.onCompleted: {
        // 设置焦点以接收键盘事件
        forceActiveFocus()
        
        // 初始化应用程序
        console.log("浮游菌采样器控制系统启动")
        console.log("分辨率: " + width + "x" + height)
        console.log("用户登录状态: " + qmlBridge.isUserLoggedIn)
    }
    
    // 应用程序关闭处理
    onClosing: function(close) {
        if (qmlBridge.isUserLoggedIn) {
            close.accepted = false
            showConfirmDialog("退出确认", "确定要退出系统吗？", function(accepted) {
                if (accepted) {
                    qmlBridge.logoutUser()
                    Qt.quit()
                }
            })
        }
    }
}